package com.chenxi.mapper;

import com.chenxi.bean.Order;
import com.chenxi.bean.User;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.io.IOException;
import java.util.List;

public interface UserMapper {

    void insert(User user) throws IOException;

    void update(User user) throws IOException;

    void delete(Integer id) throws IOException;

    List<User> findAll() throws IOException;

    User getById(Integer id) throws IOException;

    List<User> findByCondition(User user);

    List<User> findByIdList(List<Integer> idList);

    List<User> findByIds(int [] ids);

    // 查询用户信息，包含订单信息
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "birthday", column = "birthday"),
            // 根据主键id的值,调用many注解的查询语句查询到Order对象。赋值给orderList属性，返回类型（javaType = List.class）
            @Result(property = "orderList", column = "id" , javaType = List.class,
                    many = @Many(select = "com.chenxi.mapper.OrderMapper.findOrderListByUserId"))
    })
    @Select(value = "select * from user")
    List<User> findUserAndOrderList();


    @Select(value = "select * from user where id = #{id}")
    User findById(Integer id);



    // 查询用户信息，包含角色信息
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "birthday", column = "birthday"),
            // 根据主键id的值,调用many注解的查询语句查询到Role对象集合。赋值给roleList属性，返回类型（javaType = List.class）
            @Result(property = "roleList", column = "id" , javaType = List.class,
                    many = @Many(select = "com.chenxi.mapper.RoleMapper.findRoleListByUserId"))
    })
    @Select(value = "select * from user")
    List<User> findUserAndRoleList();

}
